package com.talent.jiwen_teacher.my;

import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.base.BaseActivity;
import com.talent.jiwen_teacher.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.http.result.TeacherInfo;
import com.talent.jiwen_teacher.util.SPUtil;
import com.talent.jiwen_teacher.util.Validators;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;


/**
 * Created by qyqx on 2018/8/17.
 * 时间设置
 */

public class TimeSetActivity extends BaseActivity {

    @BindView(R.id.rl_work_start)
    RelativeLayout rl_work_start;
    @BindView(R.id.rl_work_end)
    RelativeLayout rl_work_end;
    @BindView(R.id.rl_holiday_start)
    RelativeLayout rl_holiday_start;
    @BindView(R.id.rl_holiday_end)
    RelativeLayout rl_holiday_end;


    @BindView(R.id.txt_work_start)
    TextView txt_work_start;
    @BindView(R.id.txt_work_end)
    TextView txt_work_end;
    @BindView(R.id.txt_holiday_start)
    TextView txt_holiday_start;
    @BindView(R.id.txt_holiday_end)
    TextView txt_holiday_end;



    @Override
    protected int provideContentViewId() {
        return R.layout.activity_time_set;
    }

    @Override
    public String titleName() {
        return "通知时间";
    }

    @Override
    protected void initData() {

        TeacherInfo teacherInfo= (TeacherInfo) getIntent().getSerializableExtra("teacherInfo");
        if(teacherInfo!=null){
            workPosition=teacherInfo.getWorkdayStartTime();
            workEndPosition=teacherInfo.getWorkdayEndTime();
            holidayPosition=teacherInfo.getFreedayStartTime();
            holidayEndPosition=teacherInfo.getFreedayEndTime();
            txt_work_start.setText(times[workPosition]);
            txt_work_end.setText(times[workEndPosition]);
            txt_holiday_start.setText(times[holidayPosition]);
            txt_holiday_end.setText(times[holidayEndPosition]);
        }



    }

    private static final int WORK_START=1;
    private static final int WORK_END=2;
    private static final int HOLIDAY_START=3;
    private static final int HOLIDAY_END=4;

    String workStart;
    int workPosition=-1;
    String workEnd;
    int workEndPosition=-1;
    String holidayStart;
    int holidayPosition=-1;
    String holidayEnd;
    int holidayEndPosition=-1;



    private String [] times=new String[]{"0:00","1:00","2:00","3:00","4:00","5:00","6:00","7:00","8:00","9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00","24:00"};
    private List<String> timeList=new ArrayList<>(Arrays.asList(times));

    @OnClick({R.id.rl_work_start,R.id.rl_work_end,R.id.rl_holiday_start,R.id.rl_holiday_end,R.id.save_set})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_work_start:
                showWheelPicker(timeList,null,WORK_START);
//                txt_work_end.setText("");
//                workPosition=-1;

                break;
            case R.id.rl_work_end:
                if(!Validators.isEmpty(workStart)){
                    if(workPosition<timeList.size()){
                        List<String> timeList1=timeList.subList(workPosition+1,timeList.size());
                        if(timeList1.size()==0){
                            showToast("请重新选择开始时间");
                            return;
                        }
                        showWheelPicker(timeList1,null,WORK_END);
                    }else{
                        showToast("请重新选择开始时间");
                    }

                }else{
                    showToast("请先选择开始时间");
                }

                break;
            case R.id.rl_holiday_start:
                showWheelPicker(timeList,null,HOLIDAY_START);
//                txt_holiday_end.setText("");
//                holidayPosition=-1;

                break;
            case R.id.rl_holiday_end:

                if(!Validators.isEmpty(holidayStart)){
                    List<String> timeList1=timeList.subList(holidayPosition+1,timeList.size());
                    if(timeList1.size()==0){
                        showToast("请重新选择开始时间");
                        return;
                    }
                    showWheelPicker(timeList1,null,HOLIDAY_END);
                }else{
                    showToast("请先选择开始时间");
                }

                break;

            case R.id.save_set:

                saveTimeSet();

                break;
        }
    }

    private void saveTimeSet() {

        Map<String, String> map = new HashMap<>();
        map.put("teacherId", SPUtil.getUserId());
        map.put("workDayStartTime", ""+workPosition);
        map.put("workDayEndTime", ""+workEndPosition);
        map.put("freeDayStartTime", ""+holidayPosition);
        map.put("freeDayEndTime", ""+holidayEndPosition);

        Observable observable = ApiFactory.getEducationApiSingleton().updateTeacherWorkTime(SPUtil.getToken(),map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(mContext) {
            @Override
            protected void _onNext(Void v) {
                showToast("设置成功");
                EventBus.getDefault().post("refresh");
                finish();
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);

    }

    /**
     * 滚轮选择
     *
     * @param pickerList
     */
    private void showWheelPicker(final List<String> pickerList, final String str, final int style) {
        final List<String> showList = new ArrayList<>();
        int selectPosition = 0;
        for (int i = 0; i < pickerList.size(); i++) {
            showList.add(pickerList.get(i));
            if (null != str){
                if (str.equals( pickerList.get(i))) {
                    selectPosition = i;
                }
            }
        }
        OptionsPickerView pvOptions = new OptionsPickerBuilder(TimeSetActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                switch (style){
                    case WORK_START:
                        workStart=times[options1];
                        workPosition=options1;
                        txt_work_start.setText(workStart);
                        txt_work_end.setText("");
                        workEndPosition=-1;
                        break;
                    case WORK_END:
                        workEndPosition=workPosition+1+options1;
                        workEnd= pickerList.get(options1);
                        txt_work_end.setText(workEnd);
                        break;
                    case HOLIDAY_START:
                        holidayStart=times[options1];
                        holidayPosition=options1;
                        txt_holiday_start.setText(holidayStart);
                        txt_holiday_end.setText("");
                        holidayEndPosition=-1;
                        break;
                    case HOLIDAY_END:
                        holidayEnd=pickerList.get(options1);
                        txt_holiday_end.setText(holidayEnd);
                        holidayEndPosition=holidayPosition+1+options1;
                        break;

                }
            }
        }).setSubmitColor(ContextCompat.getColor(this , R.color.common_app_color))
                .setCancelColor(ContextCompat.getColor(this , R.color.text_gray_6)).build();
        pvOptions.setPicker(showList);

        if (null != str) {
            pvOptions.setSelectOptions(selectPosition);
        }
        pvOptions.show();
    }
}
